/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package webserver;

import java.util.ArrayList;

/**
 * Queue represents a implementation of a List as a FILO Stack
 * Elements are appended to the back of the list while the top item
 * will be returned the next time
 * @author Andi
 * @param <E> - Template Parameter (which class should be queueable?)
 * 
 */
public class Queue<E> {

	// Variables
	private ArrayList<E> list;

	/**
	 * Queue Default Constructor
	 */
	public Queue() {
		this.list = new ArrayList<E>();
	}

	/**
	 * Get the current size of the Queue
	 * @return int - the current element count
	 */
	public synchronized int getSize() {
		return this.list.size();
	}

	/**
	 * Add a new Item to the Queue
	 * Items are appended to the back of the list
	 * @param e - The item which should be added
	 */
	public synchronized void enqueue(E e) {
		this.list.add(e);
		this.notify();
	}

	/**
	 * Remove a item from the list
	 * @param e - The item which should be removed
	 */
	public synchronized void remove(E e) {
		if (this.list.contains(e)) {
			this.list.remove(e);
		}
	}

	/**
	 * Get the next pending Element
	 * This function will return the item on top of the list
	 * This item is the "oldest" one in the list and should now be processed
	 * @return e - The item on top
	 */
	public synchronized E getNextPendingElement() {
		if (this.list.isEmpty()) {
			return null;
		}

		return this.list.remove(0);
	}
}
